package com.lemon.cloud.mp.web;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lemon.cloud.comm.model.ResultMsg;
import com.lemon.cloud.mp.constants.ReplyTypeEnum;
import com.lemon.cloud.mp.entity.WxAutoReply;
import com.lemon.cloud.mp.service.WxAutoReplyService;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@AllArgsConstructor
@RequestMapping("/wxautoreply")
public class WxAutoReplyController {


    private final WxAutoReplyService wxAutoReplyService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param wxAutoReply 消息自动回复
     * @return
     */
    @GetMapping("/page")
    public ResultMsg<IPage<WxAutoReply>> getWxAutoReplyPage(Page<WxAutoReply> page, WxAutoReply wxAutoReply) {
        return ResultMsg.resultSuccess(wxAutoReplyService.page(page, Wrappers.query(wxAutoReply)));
    }

    /**
     * 通过id查询消息自动回复
     * @param id id
     * @return ResultMsg
     */
    @GetMapping("/{id}")
    public ResultMsg<WxAutoReply> getById(@PathVariable("id") String id) {
        return ResultMsg.resultSuccess(wxAutoReplyService.getById(id));
    }

    /**
     * 新增消息自动回复
     * @param wxAutoReply 消息自动回复
     * @return ResultMsg
     */
    @PostMapping
    @PreAuthorize("hasAuthority('mp_wxautoreply_add')")
    public ResultMsg<Boolean> save(@RequestBody WxAutoReply wxAutoReply) {
        this.jude(wxAutoReply);
        return ResultMsg.resultSuccess(wxAutoReplyService.save(wxAutoReply));
    }

    /**
     * 修改消息自动回复
     * @param wxAutoReply 消息自动回复
     * @return ResultMsg
     */
    @PutMapping
    @PreAuthorize("hasAuthority('mp_wxautoreply_edit')")
    public ResultMsg<Boolean> updateById(@RequestBody WxAutoReply wxAutoReply) {
        this.jude(wxAutoReply);
        return ResultMsg.resultSuccess(wxAutoReplyService.updateById(wxAutoReply));
    }

    /**
     * 通过id删除消息自动回复
     * @param id id
     * @return ResultMsg
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('mp_wxautoreply_del')")
    public ResultMsg<Boolean> removeById(@PathVariable("id") String id) {
        return ResultMsg.resultSuccess(wxAutoReplyService.removeById(id));
    }

    /**
     * //校验参数
     * @param wxAutoReply
     */
    public void jude(WxAutoReply wxAutoReply) {
        if (ReplyTypeEnum.MSG.getType().equals(wxAutoReply.getType())) {
            Wrapper<WxAutoReply> queryWrapper = Wrappers.<WxAutoReply>lambdaQuery().eq(WxAutoReply::getReqType,
                    wxAutoReply.getReqType());
            List<WxAutoReply> list = wxAutoReplyService.list(queryWrapper);
            if (wxAutoReply.getId() != null) {
                if (list != null && list.size() == 1) {
                    if (!list.get(0).getId().equals(wxAutoReply.getId())) {
                        throw new RuntimeException("请求消息类型重复");
                    }
                }
                if (list != null && list.size() > 1) {
                    throw new RuntimeException("请求消息类型重复");
                }
            }
        }
        if (ReplyTypeEnum.KEYWORD.getType().equals(wxAutoReply.getType())) {
            Wrapper<WxAutoReply> queryWrapper = Wrappers.<WxAutoReply>lambdaQuery()
                    .eq(WxAutoReply::getReqKey, wxAutoReply.getReqKey())
                    .eq(WxAutoReply::getRepType, wxAutoReply.getRepMate());
            List<WxAutoReply> list = wxAutoReplyService.list(queryWrapper);
            if (list != null && list.size() == 1) {
                if (!list.get(0).getId().equals(wxAutoReply.getId())) {
                    throw new RuntimeException("关键词重复");
                }
            }
            if (list != null && list.size() > 1) {
                throw new RuntimeException("关键词重复");
            }
        }
    }
}
